package com.jiepos.mpos.biz.http.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.biz.customer.service.AppCustomerInfoService;
import com.jiepos.mpos.biz.customer.service.CustomAccountChangeService;
import com.jiepos.mpos.biz.transaction.service.LogTransJYLSService;
import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.RspCodeConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCustomerInfoDao;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.common.entity.GpsEntity;
import com.jiepos.mpos.common.service.AbrBaseHttpService;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;
import com.jiepos.mpos.pay.LogAbstractService;


/**
 *
 * <p>标题: —— 交易流水</p>
 * <p>模块: 交易流水模块</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年9月15日 下午1:46:53
 * @history（历次修订内容、修订人、修订时间等）
 */

@Service
public class HttpTransService extends AbrBaseHttpService {
    @Autowired
    private HttpCommonService httpCommonService;
    @Autowired
    private LogTransJYLSService logTransJYLSService;
    @Autowired
    private AppCustomerInfoService appCustomerInfoService;
    @Autowired
    private CustomAccountChangeService customAccountChangeService;
    @Autowired
    private LogAbstractService logAbstractService;
    @Autowired
    private AppCustomerInfoDao appCustomerInfoDao;


    /**
     * 查询交易流水
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param begin_tm
     * @param end_tm
     * @param curr_page
     * @param rows_perpage
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getLogTrans(HttpServletRequest request, String login_id, String random_number,
                           String custom_no, String begin_tm, String end_tm, String curr_page,
                           String rows_perpage, String sign, GpsEntity gpsEntity) {
        //验签
        String serviceData =  StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(curr_page)
                + StringUtils.objToString(rows_perpage);
        Map checkSignMap = httpCommonService.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httptrans/getLogTrans.do", gpsEntity, "交易流水接口");;//.checkCommonSign(request, serviceData, sign, "交易流水接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map data = new HashMap();
            //login_id 登录账号
            data.put("login_id", login_id);
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            List trans_list = logTransJYLSService.getLogTrans(request,login_id,custom_no,begin_tm,end_tm,curr_page,rows_perpage,gpsEntity);
            Map dataMap = new HashMap();
            dataMap.put("trans_list",trans_list);
            subLog.debug("查询交易流水:{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
       }
        return checkSignMap;
    }

    /**
     * 查询佣金流水
     * @param request
     * @param response
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param begin_tm
     * @param end_tm
     * @param curr_page
     * @param rows_perpage
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getCommissionTrans(HttpServletRequest request, HttpServletResponse response, String login_id,
                           String random_number, String custom_no, String begin_tm, String end_tm,
                           String curr_page, String rows_perpage, String sign, GpsEntity gpsEntity){
        String serviceData =  StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(curr_page)
                + StringUtils.objToString(rows_perpage);
        Map checkSignMap = httpCommonService.getLogClientKeyEntity(request, gpsEntity.getSerial_number(),
                sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httptrans/getCommissionTrans.do", gpsEntity, "查询佣金流水");
        subLog.info("查询佣金流水接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            if(StringUtils.isBlank(curr_page) || StringUtils.isBlank(rows_perpage)){
                subLog.info("参数为空");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            List trans_list = customAccountChangeService.getCommissionFlowList(customInf.getCustom_uuid(),begin_tm,end_tm,curr_page,rows_perpage);
            dataMap.put("trans_list",trans_list);
            subLog.info("查询佣金流水完成，接口已返回");
            return super.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 查询分润流水
     * @param request
     * @param response
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param begin_tm
     * @param end_tm
     * @param curr_page
     * @param rows_perpage
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getProfitTrans(HttpServletRequest request, HttpServletResponse response, String login_id,
                                  String random_number, String custom_no, String begin_tm, String end_tm,
                                  String curr_page, String rows_perpage, String sign, GpsEntity gpsEntity){
        String serviceData =  StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(curr_page)
                + StringUtils.objToString(rows_perpage);
        Map checkSignMap = httpCommonService.getLogClientKeyEntity(request, gpsEntity.getSerial_number(),
                sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httptrans/getProfitTrans.do", gpsEntity, "查询分润流水");
        subLog.info("查询分润流水接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            if(StringUtils.isBlank(curr_page) || StringUtils.isBlank(rows_perpage)){
                subLog.info("参数为空");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            List trans_list = customAccountChangeService.getProfitFlowList(customInf.getCustom_uuid(),begin_tm,end_tm,curr_page,rows_perpage);
            dataMap.put("trans_list",trans_list);
            subLog.info("查询分润流水完成，接口已返回");
            return super.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 查询账户变动明细
     * @param request
     * @param response
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param begin_tm
     * @param end_tm
     * @param curr_page
     * @param rows_perpage
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getActChangeTrans(HttpServletRequest request, HttpServletResponse response, String login_id,
                              String random_number, String custom_no, String begin_tm, String end_tm,
                              String curr_page, String rows_perpage, String sign, GpsEntity gpsEntity){
        String serviceData =  StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(curr_page)
                + StringUtils.objToString(rows_perpage);
        Map checkSignMap = httpCommonService.getLogClientKeyEntity(request, gpsEntity.getSerial_number(),
                sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httptrans/getActChangeTrans.do", gpsEntity, "查询账户变动明细");
        subLog.info("查询账户变动明细接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            if(StringUtils.isBlank(curr_page) || StringUtils.isBlank(rows_perpage)){
                subLog.info("参数为空");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //查询账户变动明细
            List trans_list = customAccountChangeService.getAccountChangeList(customInf.getCustom_uuid(),begin_tm,end_tm,curr_page,rows_perpage);
            dataMap.put("trans_list",trans_list);
            subLog.info("查询账户变动明细完成，接口已返回");
            return super.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 查询提现流水
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getAbstractTrans(HttpServletRequest request, String login_id, String random_number, String custom_no,String begin_tm,String end_tm,String curr_page,String rows_perpage,String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number())+ StringUtils.objToString(custom_no)+ StringUtils.objToString(curr_page)+ StringUtils.objToString(rows_perpage);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number,
                gpsEntity.getVersion(), serviceData, login_id, "httpcustom/getAbstractTrans.do", gpsEntity, "查询提现流水接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //查询提现流水
            List<Map<String,Object>> abstract_list = this.getAbstractTrans(login_id,custom_no,begin_tm,end_tm,curr_page,rows_perpage,customInf.getCustom_uuid());
            Map dataMap = new HashMap();
            dataMap.put("abstract_list",abstract_list);
            subLog.debug("查询提现流水:{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
    }
        return checkSignMap;
    }
    /**
     * 查询提现流水
     * @param login_id
     * @param custom_no
     * @param begin_tm
     * @param end_tm
     * @param curr_page
     * @param rows_perpage
     * @param custom_uuid
     * @return
     */
    public List getAbstractTrans(String login_id, String custom_no, String begin_tm, String end_tm, String curr_page, String rows_perpage, String custom_uuid) {
        Pagination pagination = new  Pagination();
        //分页
        pagination.setPage(Integer.parseInt(curr_page));
        pagination.setRows(Integer.parseInt(rows_perpage));
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" select ")
                /*.append(" t1.apply_dt as \"apply_dt\", ")*/
                .append(" TO_CHAR(TO_DATE (T1.APPLY_DT || T1.APPLY_TS,'yyyy-MM-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') AS \"apply_dt\",")
                .append("  TO_CHAR(NVL(t1.apply_amt , 0.00), 'fm9999999990.00') AS \"apply_amt\",")
                .append("  TO_CHAR(NVL(t1.withdraw_fee , 0.00), 'fm9999999990.00') AS \"withdraw_fee\",")
                .append("  TO_CHAR(NVL(t1.stl_amt , 0.00), 'fm9999999990.00') AS \"stl_amt\",")
                .append(" t1.rsp_code || '-'|| rsp_msg \"rsp_code\", ")//提现结果
                /*.append(" t1.stl_code || '-'|| stl_msg \"stl_code\", ")//清算结果*/
                .append("NVL2 ( t1.stl_code||t1.stl_msg,t1.stl_code || '-'|| t1.stl_msg, '') as \"stl_code\",")//清算结果
                .append(" t1.audit_stlflag ||'-'|| t2.code_desc as \"audit_stlflag\", ")
                .append(" TO_CHAR(TO_DATE(t1.audit_dt || t1.audit_ts, 'yyyyMMddHH24miss'),  'yyyy-MM-dd HH24:mi:ss') AS \"audit_dts\" ")
             /*   .append(" TO_CHAR(TO_DATE( t1.audit_dts, 'yyyyMMddHH24miss')) as \"audit_dts\" ")*/
                .append(" from  ")
                .append(" "+ DBConstant.Table.T_APP_LOG_ABSTRACT+" t1  ")
                .append("  LEFT JOIN "+ DBConstant.Table.T_APP_CODE_LIST+" T2  ")
                .append(" ON t1.audit_stlflag = t2.code_val and t2.code_tp='STLST' ")
                .append("where t1.custom_uuid = ?");
        List param = new ArrayList();
        param.add(custom_uuid);
        if (!StringUtils.isBlank(begin_tm)) {
            sqlBuf.append("and t1.apply_dt >= ? ");
            param.add(begin_tm);
        }
        if (!StringUtils.isBlank(end_tm)) {
            sqlBuf.append("and t1.apply_dt <= ? ");
            param.add(end_tm);
        }
        sqlBuf.append(" order by t1.apply_dt desc , t1.apply_ts desc  ");
        pagination.queryString = sqlBuf.toString();
        pagination = appCustomerInfoDao.getPagination(pagination, "t1", param);
        List<Map<String,String>> resultList =  pagination.getOldData();
        return resultList;
    }
}